import { createRouter, createWebHistory } from 'vue-router'
import index from '../views/Index.vue'
import login from '../views/Login.vue'
import register from '../views/Register.vue'
import wbadd from '../views/Weibo_add.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      name: 'index',
      component: index,
      meta: {
        needLogin:true
      }
    },
    {
      path: '/login',
      name: 'login', 
      component: login,
    },
    {
      path: '/reg',
      name: 'register',
      component: register,
    },
    {
      path: '/wb/add',
      name: 'wbadd',
      component: wbadd,
      meta: {
        needLogin:true
      }
    }
      
  ],
});

router.beforeEach((to, from, next) => {
  if(to.meta.needLogin && !sessionStorage.getItem('curUser')){
    alert('请先登录');
    next('/login');
  }
  else
    next();
});

export default router
